package Controller.admin.manage;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Controller.admin.AdminBaseServlet;
import Entity.Book;
import Services.WebService;

import java.util.ArrayList;

public class BookServlet extends AdminBaseServlet {
	@Override
	protected void processRequest(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) throws ServletException,
			IOException {
		if (action == null || action.equals("list")) {
			this.listBooks(request, response, webServices, action);
		} else if (action.equals("new")) {
			this.newBook(request, response, webServices, action);
		} else if (action.equals("show")) {
			this.showBook(request, response, webServices, action);
		} else if (action.equals("edit")) {
			this.editBook(request, response, webServices, action);
		} else if (action.equals("delete")) {
			this.deleteBook(request, response, webServices, action);
		}
	}

	private void newBook(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) throws IOException {
		String submit = request.getParameter("submit");
		if(submit!= null && submit.equals("true")){
			String title = request.getParameter("title");
			String description = request.getParameter("description");
	
			String temp = request.getParameter("price");
			boolean isPriceNotFloat = !WebService.isFloat(temp);
			Float price = (temp == null || isPriceNotFloat)?-1:Float.parseFloat(temp);
	
			temp = request.getParameter("quantity");
			boolean isQuantityNotInt = !WebService.isInteger(temp);
			int quantity = (temp == null || isQuantityNotInt)?-1:Integer.parseInt(temp);
			
			String publisher = request.getParameter("publisher");
			String language = request.getParameter("language");
	
			Book book = new Book();
				
			if(title != null)
				book.setTitle(title);
			
			if(description != null)
				book.setDescription(description);
			
			if(!isPriceNotFloat && price > 0.0)
				book.setPrice(price);
			
			if(!isQuantityNotInt && quantity >= 0)
				book.setQuantity(quantity);
			
			if(publisher != null)
				book.setPublisher(publisher);
			
			if(language != null)
				book.setLanguage(language);
			
			
			if (request.getMethod() == "POST" && !isPriceNotFloat && !isQuantityNotInt && 
					title != null && description != null && price > 0.0 && quantity >= 0 && publisher != null && language != null) {
				book.setNoOfView(0);
				book.setNoOfPurchase(0);
	
				book = webServices.getEntityManager().newBook(book);
				if (book!=null && book.getId()>0) {
					response.sendRedirect(webServices.getBaseURL()+"/admin/manage/book?action=show&id="+book.getId());
				}
			} else {
				request.setAttribute("book", book);
				request.setAttribute("isQuantityNotInt", isQuantityNotInt);
				request.setAttribute("isPriceNotFloat", isPriceNotFloat);
				
				action = "failed";
				this.forward(request, response, webServices, action, "/admin/manage/book/new.jsp");
			}
		} else {
			this.forward(request, response, webServices, action, "/admin/manage/book/new.jsp");
		}
	}

	private void listBooks(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) {
		ArrayList<Book> bookList = webServices.getEntityManager().getBooks();
		request.setAttribute("books", bookList);
		this.forward(request, response, webServices, action, "/admin/manage/book/list.jsp");
	}

	private void showBook(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) {
		String temp = request.getParameter("bookID");
		boolean isBookIDNotInt = !WebService.isInteger(temp);
		int bookID = (temp == null || isBookIDNotInt)?-1:Integer.parseInt(temp);
		
		if(!isBookIDNotInt){
			Book book = webServices.getEntityManager().getBook(bookID);
	
			request.setAttribute("book", book);
			this.forward(request, response, webServices, action, "/admin/manage/book/list.idv.jsp");
		}else {
			//Not found
			try {
				response.sendRedirect(webServices.getBaseURL()+"/admin/manage/book?action=list");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void editBook(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) throws IOException {
		String temp = request.getParameter("id");
		boolean isIDNotInt = !WebService.isInteger(temp);
		int bookID = (temp == null || isIDNotInt)?-1:Integer.parseInt(temp);
		
		String submit = request.getParameter("submit");
		if(submit!= null && submit.equals("true")){
			String title = request.getParameter("title");
			String description = request.getParameter("description");
			
			temp = request.getParameter("price");
			boolean isPriceNotFloat = !WebService.isFloat(temp);
			Float price = (temp == null || isPriceNotFloat)?-1:Float.parseFloat(temp);
			
			temp = request.getParameter("quantity");
			boolean isQuantityNotInt = !WebService.isInteger(temp);
			int quantity = (temp == null || isQuantityNotInt)?-1:Integer.parseInt(temp);
			
			String publisher = request.getParameter("publisher");
			String language = request.getParameter("language");
			
			temp = request.getParameter("noOfView");
			boolean isNoOfViewNotInt = !WebService.isInteger(temp);
			int noOfView  = (temp == null || isNoOfViewNotInt)?-1:Integer.parseInt(temp);

			temp = request.getParameter("noOfPurchase");
			boolean isNoOfPurchaseNotInt = !WebService.isInteger(temp);
			int noOfPurchase = (temp == null || isNoOfPurchaseNotInt)?-1:Integer.parseInt(temp);

			Book book = new Book();
			
			if(!isIDNotInt)
				book.setId(bookID);
				
			if(title != null)
				book.setTitle(title);
			
			if(description != null)
				book.setDescription(description);
			
			if(!isPriceNotFloat && price > 0.0)
				book.setPrice(price);
			
			if(!isQuantityNotInt && quantity >= 0)
				book.setQuantity(quantity);
			
			if(publisher != null)
				book.setPublisher(publisher);
			
			if(language != null)
				book.setLanguage(language);
			
			if(!isNoOfViewNotInt && noOfView >= 0)
				book.setNoOfView(noOfView);
			
			if(!isNoOfPurchaseNotInt && noOfPurchase >= 0)
				book.setNoOfPurchase(noOfPurchase);
			
			
			if (request.getMethod() == "POST" && !isIDNotInt && !isNoOfPurchaseNotInt && !isNoOfViewNotInt && !isPriceNotFloat && !isQuantityNotInt && 
					title != null && description != null && price > 0.0 && quantity >= 0 && publisher != null && language != null && noOfView >= 0 && noOfPurchase >= 0) {
				webServices.getEntityManager().updateBook(book);
				response.sendRedirect(webServices.getBaseURL()+"/admin/manage/book?action=show&id="+book.getId());
				return;
			} else {
				request.setAttribute("book", book);
				request.setAttribute("isQuantityNotInt", isQuantityNotInt);
				request.setAttribute("isPriceNotFloat", isPriceNotFloat);
				request.setAttribute("isNoOfViewNotInt", isNoOfViewNotInt);
				request.setAttribute("isNoOfPurchaseNotInt", isNoOfPurchaseNotInt);
				
				action = "failed";
				this.forward(request, response, webServices, action, "/admin/manage/book/edit.jsp");
			}
		} else if(bookID>0){
			Book book = webServices.getEntityManager().getBook(bookID);
			request.setAttribute("book", book);
			this.forward(request, response, webServices, action, "/admin/manage/book/edit.jsp");
		} else {
			//Bad ID
			response.sendRedirect(webServices.getBaseURL()+"/admin/manage/book?action=list");
		}
	}

	private void deleteBook(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) {
		String temp = request.getParameter("id");
		boolean isIDNotInt = !WebService.isInteger(temp);
		int bookID = (temp == null || isIDNotInt)?-1:Integer.parseInt(temp);

		Book book = new Book();
			String submit = request.getParameter("submit");
		if(submit!= null && submit.equals("true") && request.getMethod() == "POST" && !isIDNotInt && bookID > 0) {
			book.setId(bookID);
			
			webServices.getEntityManager().deleteBook(book);

			action = "done"; 
			this.forward(request, response, webServices, action, "/admin/manage/book/delete.jsp");
		} else {
			book = webServices.getEntityManager().getBook(bookID);
			
			request.setAttribute("book", book);
			this.forward(request, response, webServices, action, "/admin/manage/book/delete.jsp");
		}
		
	}
}
